package com.example.orderfood2;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.orderfood2.DatabaseManager.DatabaseManager;
import com.example.orderfood2.Repository.ManagerRepository;
import com.example.orderfood2.managerOperate.AddClassActivity;
import com.example.orderfood2.managerOperate.AddStudentActivity;
import com.example.orderfood2.managerOperate.AddTeacherActivity;
import com.example.orderfood2.util.DBUtil;
import com.example.orderfood2.viewAllInformation.ViewAllClassInformationActivity;
import com.example.orderfood2.viewAllInformation.ViewAllStudentInformationActivity;
import com.example.orderfood2.viewAllInformation.ViewAllTeacherInformationActivity;

public class ManageActivity extends AppCompatActivity {
//    private DBUtil dbUtil;
    private ManagerRepository dbUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dbUtil= DatabaseManager.getInstance(this).getManagerRepository();
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_manage);
        //获取登录页面传来的username
        TextView manage_name=findViewById(R.id.manage_name);
        /// 获取传递过来的 Intent 对象
        android.content.Intent intent = getIntent();
        // 获取传递的 account 数据
        String account=intent.getStringExtra("account");
        manage_name.setText(account);
        //        加载菜单条
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
//      退出代码
        Button manage_to_login=findViewById(R.id.manage_to_login);
        manage_to_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(ManageActivity.this, MainActivity.class);
                // 设置标志以清除任务栈（关键修改）
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(i);
                // 结束当前Activity（确保退出）
                finish();
            }
        });
        //给左上图标添加点击事件，并完成左侧边栏弹出
        LinearLayout leftSidebar = findViewById(R.id.Left_sidebar);
        View overlay=findViewById(R.id.overlay);
//        用数组传值
        final int[] targetWidth = {0};
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 获取参数对象
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) leftSidebar.getLayoutParams();

                // 检查当前宽度，如果是0dp就改为300dp，否则改回0dp（实现点击切换）
                if (params.width == 0) {
                    // 将300dp转换为像素
//                    getResources()用于访问屏幕信息，getDisplayMetrics() 返回 DisplayMetrics 对象，包含当前设备的屏幕信息density：1dp 对应的像素值（px）
                    targetWidth[0] = (int) (300 * getResources().getDisplayMetrics().density);
                    // 创建一个ValueAnimator，从0过渡到目标宽度,即给修改params.width添加动画，就是params.width缓慢从0到300，ValueAnimator 可以在指定的时间内，从一个值平滑地过渡到另一个值
                    ValueAnimator animator = ValueAnimator.ofInt(0, targetWidth[0]);
                    animator.setDuration(300); // 动画持续时间，单位为毫秒
//                    setInterpolator(new LinearInterpolator())：设置插值器为 LinearInterpolator，使动画匀速进行
                    animator.setInterpolator(new LinearInterpolator()); // 设置插值器，使动画匀速进行
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            // 获取当前动画的值
                            int value = (int) animation.getAnimatedValue();
                            // 更新布局参数的宽度
                            params.width = value;
                            leftSidebar.setLayoutParams(params);
                        }
                    });
                    animator.start(); // 启动动画
                    overlay.setVisibility(View.VISIBLE);//将隐藏改为可以操作
                }
            }
        });
        //添加点击事件，当用户触摸大于300的位置时，leftSidebar 由300到0
        overlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取参数对象
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) leftSidebar.getLayoutParams();
                if (params.width > 0){
                    // 创建一个ValueAnimator，从当前宽度过渡到0
                    ValueAnimator animator = ValueAnimator.ofInt(params.width, 0);
                    animator.setDuration(300); // 动画持续时间，单位为毫秒
                    animator.setInterpolator(new LinearInterpolator()); // 设置插值器，使动画匀速进行
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            // 获取当前动画的值
                            int value = (int) animation.getAnimatedValue();
                            // 更新布局参数的宽度
                            params.width = value;
                            leftSidebar.setLayoutParams(params);
                        }
                    });
                    animator.start(); // 启动动画
                    overlay.setVisibility(View.GONE);//将可以操作改为隐藏
                }
            }
        });
        //密码修改
        Button manage_change_password=findViewById(R.id.manage_change_password);
        manage_change_password.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //添加Dialog组件
                PasswordChangeDialog.showPasswordChangeDialog(ManageActivity.this,account);
            }
        });
//        用户注销
//        dbUtil=new DBUtil(this);
        Button delete_manager=findViewById(R.id.delete_manager);
        delete_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbUtil.DeleteManager(account);
                Toast.makeText(ManageActivity.this,"用户"+account+"成功注销",Toast.LENGTH_SHORT).show();
                Intent intent1=new Intent(ManageActivity.this,MainActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent1);
                finish();
            }
        });
        //查看三个按钮的点击监听事件
        Button btn_teachers=findViewById(R.id.btn_teachers);
        Button btn_students=findViewById(R.id.btn_students);
        Button btn_classes=findViewById(R.id.btn_classes);
        btn_classes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(ManageActivity.this, ViewAllClassInformationActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(i);

            }
        });
        btn_students.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(ManageActivity.this, ViewAllStudentInformationActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(i);

            }
        });
        btn_teachers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(ManageActivity.this, ViewAllTeacherInformationActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(i);

            }
        });
    }
    //    将R.menu.manage_menu中的布局给toolbar
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.manage_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //给右上角的选项添加事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id=item.getItemId();
        if(id==R.id.manage_add_teacher){
            Intent AddTeacherActivity=new Intent(ManageActivity.this, AddTeacherActivity.class);
            startActivity(AddTeacherActivity);
        }
        else if(id==R.id.manage_add_student){
            Intent AddStudentActivity=new Intent(ManageActivity.this, AddStudentActivity.class);
            startActivity(AddStudentActivity);
        } else if (id==R.id.manage_add_class) {
            Intent AddClassActivity=new Intent(ManageActivity.this, AddClassActivity.class);
            startActivity(AddClassActivity);
        }
        return super.onOptionsItemSelected(item);
    }
}